package com.wwwzh.himalayafm.fragment.disfragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.callback.IsScrollButtonListener;
import com.wwwzh.himalayafm.fragment.disfragment.anchorfragment.adapter.AnHttpService;
import com.wwwzh.himalayafm.fragment.disfragment.anchorfragment.adapter.AnchorEntity;
import com.wwwzh.himalayafm.fragment.disfragment.anchorfragment.adapter.Anchoradapter;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 * 主播界面
 */
public class AnchorFragment extends Fragment implements Callback<AnchorEntity>, IsScrollButtonListener {


    private RecyclerView recyclerView;
    private int pageNum = 1;
    private AnHttpService service;
    private Anchoradapter anchoradapter;

    public AnchorFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_anchor, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_a);
        //加载数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(AnHttpService.class);

        service.getAnchorEntity("android", pageNum).enqueue(this);
        anchoradapter = new Anchoradapter(getContext(), this);
        recyclerView.setAdapter(anchoradapter);
    }

    @Override
    public void onResponse(Call<AnchorEntity> call, Response<AnchorEntity> response) {
        //Toast.makeText(getContext(),response.body().getList().get(0).getTitle(),Toast.LENGTH_LONG).show();
        if (response.body().getList()!=null) {
            anchoradapter.addAll(response.body().getList());
        }

    }

    @Override
    public void onFailure(Call<AnchorEntity> call, Throwable t) {

    }

    @Override
    public void isScrollbutton() {
        pageNum++;
        service.getAnchorEntity("android",pageNum).enqueue(this);
    }
}
